package com.ll.notice.Factory.impl;

import com.ll.notice.Factory.HospitalFactory;
import com.ll.notice.entity.SmsNotice;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.Logger;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;


public class DongbeiGuojiHospitalFactoryImpl implements HospitalFactory {

    private final static Logger log = Logger.getLogger(DongbeiGuojiHospitalFactoryImpl.class);
    @Override
    public void sendSms(SmsNotice smsNotice) {

        log.info("参数:" + smsNotice);
        String url       = "http://192.168.199.37:8889/mt";

        //apiKey和APISecret
        String apiKey    = "501345200001";
        String apiSecret = "90ACA91222D24189944C4684249D7375";

        //接受手机号
        String mobile    = smsNotice.getReceiverMobile();
        //短信内容(【签名】+短信内容)，系统提供的测试签名和内容，如需要发送自己的短信内容请在启瑞云平台申请短信签名
        String message   = "【中一东北国际医院】" + smsNotice.getSmsContent();
        StringBuilder sb = new StringBuilder(2000);
        sb.append(url);
        sb.append("?dc=15");
        try {
            sb.append("&sm=").append(URLEncoder.encode(message, "utf8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        sb.append("&da=").append(mobile);
        sb.append("&un=").append(apiKey);
        sb.append("&pw=").append(apiSecret);
        sb.append("&tf=3&rd=1&rf=2");   //短信内容编码为 urlencode+utf8

        String request = sb.toString();
        System.out.println("字符："+request);


        CloseableHttpClient client = HttpClients.createDefault();

        HttpGet httpGet = new HttpGet(request);

        CloseableHttpResponse response = null;
        try {
            response = client.execute(httpGet);
        } catch (IOException ioException) {
            ioException.printStackTrace();
        }

        String respStr;

        HttpEntity entity = response.getEntity();
        if(entity != null) {
            try {
                respStr = EntityUtils.toString(entity, "UTF-8");
                log.info("消息体返回：" + respStr);
            } catch (IOException ioException) {
                ioException.printStackTrace();
            }
        }
    }
}
